Suggested Citation:
Mendez C. (2020). Making maps in R: Using the sf and tmap Packages. R Studio/RPubs. Available at https://rpubs.com/quarcs-lab/tutorial-maps-in-r
This work is licensed under the Creative Commons Attribution-Share Alike 4.0 International License.
knitr::opts_chunk$set(echo = TRUE, warning=FALSE)
library(tidyverse) # Modern data science workflow
library(sf) # Simple features for R
library(tmap) # Thematic Maps
library(tmaptools) # Thematic Maps Tools
library(RColorBrewer) # ColorBrewer Palettes
library(leaflet) # Interactive web maps
library(rgdal) # Bindings for the Geospatial Data Abstraction Library
library(rgeos) # Interface to Geometry Engine - Open Source
# Change the presentation of decimal numbers to 4 and avoid scientific notation
options(prompt="R> ", digits=4, scipen=999)The non-spatial datafile is from:
The spatial (shapefile) is from:
Observations: 67
Variables: 7
$ id [3m[38;5;246m<int>[39m[23m 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12…
$ growth [3m[38;5;246m<dbl>[39m[23m 0.0254, -0.0254, 0.0773, 0.2583, 0…
$ base [3m[38;5;246m<dbl>[39m[23m 6.157, 5.900, 5.896, 5.315, 5.923,…
$ T [3m[38;5;246m<dbl>[39m[23m 3.585, 2.896, 2.763, 3.140, 2.785,…
$ E [3m[38;5;246m<dbl>[39m[23m 5.216, 3.458, 4.995, 3.478, 4.805,…
$ G [3m[38;5;246m<dbl>[39m[23m 6.192, 6.048, 5.950, 5.354, 5.940,…
$ Coastal [3m[38;5;246m<int>[39m[23m 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0…
Observations: 67
Variables: 21
$ ObjectID [3m[38;5;246m<int>[39m[23m 1946, 1949, 1952, 1957, 2116, 2…
$ NAME [3m[38;5;246m<chr>[39m[23m "Adana", "Adiyaman", "Afyon", "…
$ COUNTRY [3m[38;5;246m<chr>[39m[23m "Turkey", "Turkey", "Turkey", "…
$ ISO_CODE [3m[38;5;246m<chr>[39m[23m "TR01", "TR02", "TR03", "TR04",…
$ ISO_CC [3m[38;5;246m<chr>[39m[23m "TR", "TR", "TR", "TR", "TR", "…
$ ISO_SUB [3m[38;5;246m<chr>[39m[23m "01", "02", "03", "04", "05", "…
$ ADMINTYPE [3m[38;5;246m<chr>[39m[23m "Province", "Province", "Provin…
$ DISPUTED [3m[38;5;246m<int>[39m[23m 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ NOTES [3m[38;5;246m<chr>[39m[23m NA, NA, NA, NA, NA, NA, NA, NA,…
$ AUTONOMOUS [3m[38;5;246m<int>[39m[23m 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ COUNTRYAFF [3m[38;5;246m<chr>[39m[23m "Turkey", "Turkey", "Turkey", "…
$ CONTINENT [3m[38;5;246m<chr>[39m[23m "Asia", "Asia", "Asia", "Asia",…
$ Land_Type [3m[38;5;246m<chr>[39m[23m "Primary land", "Primary land",…
$ Land_Rank [3m[38;5;246m<int>[39m[23m 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5…
$ Shape_Leng [3m[38;5;246m<dbl>[39m[23m 8.589, 5.355, 6.698, 6.553, 4.8…
$ Shape_Area [3m[38;5;246m<dbl>[39m[23m 1.6029, 0.8622, 1.4795, 1.1537,…
$ idnum [3m[38;5;246m<int>[39m[23m 1, 2, 3, 4, 5, 7, 8, 9, 10, 11,…
$ province [3m[38;5;246m<chr>[39m[23m "Adana", "Adiyaman", "Afyon", "…
$ id [3m[38;5;246m<dbl>[39m[23m 1, 2, 3, 4, 5, 7, 8, 9, 10, 11,…
$ name_esri [3m[38;5;246m<chr>[39m[23m NA, NA, NA, NA, NA, NA, NA, NA,…
$ geometry [3m[38;5;246m<POLYGON [°]>[39m[23m POLYGON ((36.44 38.22, …
Coordinate Reference System:
EPSG: 4326
proj4string: "+proj=longlat +datum=WGS84 +no_defs"
No need to transform any data because both datasets share a common variable id
Coordinate Reference System:
EPSG: 4326
proj4string: "+proj=longlat +datum=WGS84 +no_defs"
Observations: 67
Variables: 27
$ id [3m[38;5;246m<dbl>[39m[23m 1, 2, 3, 4, 5, 7, 8, 9, 10, 11,…
$ growth [3m[38;5;246m<dbl>[39m[23m 0.0254, -0.0254, 0.0773, 0.2583…
$ base [3m[38;5;246m<dbl>[39m[23m 6.157, 5.900, 5.896, 5.315, 5.9…
$ T [3m[38;5;246m<dbl>[39m[23m 3.585, 2.896, 2.763, 3.140, 2.7…
$ E [3m[38;5;246m<dbl>[39m[23m 5.216, 3.458, 4.995, 3.478, 4.8…
$ G [3m[38;5;246m<dbl>[39m[23m 6.192, 6.048, 5.950, 5.354, 5.9…
$ Coastal [3m[38;5;246m<int>[39m[23m 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0…
$ ObjectID [3m[38;5;246m<int>[39m[23m 1946, 1949, 1952, 1957, 2116, 2…
$ NAME [3m[38;5;246m<chr>[39m[23m "Adana", "Adiyaman", "Afyon", "…
$ COUNTRY [3m[38;5;246m<chr>[39m[23m "Turkey", "Turkey", "Turkey", "…
$ ISO_CODE [3m[38;5;246m<chr>[39m[23m "TR01", "TR02", "TR03", "TR04",…
$ ISO_CC [3m[38;5;246m<chr>[39m[23m "TR", "TR", "TR", "TR", "TR", "…
$ ISO_SUB [3m[38;5;246m<chr>[39m[23m "01", "02", "03", "04", "05", "…
$ ADMINTYPE [3m[38;5;246m<chr>[39m[23m "Province", "Province", "Provin…
$ DISPUTED [3m[38;5;246m<int>[39m[23m 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ NOTES [3m[38;5;246m<chr>[39m[23m NA, NA, NA, NA, NA, NA, NA, NA,…
$ AUTONOMOUS [3m[38;5;246m<int>[39m[23m 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
$ COUNTRYAFF [3m[38;5;246m<chr>[39m[23m "Turkey", "Turkey", "Turkey", "…
$ CONTINENT [3m[38;5;246m<chr>[39m[23m "Asia", "Asia", "Asia", "Asia",…
$ Land_Type [3m[38;5;246m<chr>[39m[23m "Primary land", "Primary land",…
$ Land_Rank [3m[38;5;246m<int>[39m[23m 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5…
$ Shape_Leng [3m[38;5;246m<dbl>[39m[23m 8.589, 5.355, 6.698, 6.553, 4.8…
$ Shape_Area [3m[38;5;246m<dbl>[39m[23m 1.6029, 0.8622, 1.4795, 1.1537,…
$ idnum [3m[38;5;246m<int>[39m[23m 1, 2, 3, 4, 5, 7, 8, 9, 10, 11,…
$ province [3m[38;5;246m<chr>[39m[23m "Adana", "Adiyaman", "Afyon", "…
$ name_esri [3m[38;5;246m<chr>[39m[23m NA, NA, NA, NA, NA, NA, NA, NA,…
$ geometry [3m[38;5;246m<POLYGON [°]>[39m[23m POLYGON ((36.44 38.22, …
The shape dat_map is invalid. See sf::st_is_valid
The shape dat_map is invalid. See sf::st_is_valid
The shape dat_map is invalid. See sf::st_is_valid
tm_shape(dat_map) + tm_fill("T") +
tm_layout(legend.outside = TRUE, frame = FALSE) +
tm_legend(text.size = 0.3) The shape dat_map is invalid. See sf::st_is_valid
See color reference here
tm_shape(dat_map) +
tm_fill("T", palette = "Greens") +
tm_layout(legend.outside = TRUE, frame = FALSE)The shape dat_map is invalid. See sf::st_is_valid
tm_shape(dat_map) +
tm_fill("T", palette = "-Greens") +
tm_layout(legend.outside = TRUE, frame = FALSE)The shape dat_map is invalid. See sf::st_is_valid
tm_shape(dat_map) +
tm_fill("T", palette = "viridis") +
tm_layout(legend.outside = TRUE, frame = FALSE)The shape dat_map is invalid. See sf::st_is_valid
tm_shape(dat_map) +
tm_fill("T", palette = "viridis") +
tm_layout(legend.outside = TRUE, frame = FALSE) +
tm_text("province", size = "Shape_Area", auto.placement = F, legend.size.show = FALSE) The shape dat_map is invalid. See sf::st_is_valid
tm_shape(dat_map) +
tm_fill("T", palette = "viridis") +
tm_layout(legend.outside = TRUE, frame = FALSE) +
tm_text("province", size = "Shape_Area", auto.placement = F, legend.size.show = FALSE) +
tm_scale_bar(position = c("RIGHT", "BOTTOM"))The shape dat_map is invalid. See sf::st_is_valid
Enter “style =” followed by one of the options below.
tm_shape(dat_map) +
tm_fill("T",
style = "quantile",
palette = "Reds"
) +
tm_layout(
legend.outside = TRUE,
frame = FALSE)Change the number of intervals in the color scheme and how the intervals are spaced. Changing the number of intervals n = 7. So, we have 7 shades instead of the default 5.
You can edit the borders of the shapefile with the tm_borders() function which has many arguments. alpha denotes the level of transparency on a scale from 0 to 1 where 0 is completely transparent.
tm_shape(dat_map) +
tm_fill("T",
style = "quantile",
palette = "viridis"
) +
tm_borders(alpha=.4) +
tm_compass(type = "8star",
position = c("RIGHT", "BOTTOM"),
show.labels = 2,
text.size = 0.35) +
tm_layout(legend.outside = TRUE,
frame = FALSE)The shape dat_map is invalid. See sf::st_is_valid
tmap mode set to interactive viewing
tm_shape(dat_map) +
tm_fill("T",
palette = "viridis",
id="province",
popup.vars=c("T", "growth", "base", "E", "G")
) +
tm_legend(outside=TRUE) +
tm_layout(frame = FALSE) The shape dat_map is invalid. See sf::st_is_valid
tmap mode set to plotting
Tennekes, M., 2018, tmap: Thematic Maps in R, Journal of Statistical Software, 84(6), 1-39
https://marcinstepniak.eu/post/interactive-choropleth-maps-with-r-and-tmap-part-i/
https://rstudio-pubs-static.s3.amazonaws.com/229482_b5b182f650434a36ace4eeef2d2bcd83.html#17
https://mran.microsoft.com/snapshot/2017-12-11/web/packages/tmap/vignettes/tmap-nutshell.html